package com.shine.icis.config;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.shine.cotrun.web.comm.SpringContextLoader;
import com.shine.icis.blo.IETLLogService;
import com.shine.icis.blo.ISubmitConfService;
import com.shine.icis.dto.DictOption;
import com.shine.icis.hbean.SubmitConf;


/**
 * com.shine.ermp.syscfg.SysCodeDictLoader.java
 * <p>
 * Title: 系统数据字典
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Copyright: SHINETECH
 * </p>
 * <p>
 * Company: SHINETECH
 * </p>
 * 
 * @author zsy
 * @version
 */
public class SysCodeDictLoader {

	private static final Log log = LogFactory.getLog(SysCodeDictLoader.class);

	private static SysCodeDictLoader singleton;
	
	private Map<String, DictOption> etlProcOptions;//ETL过程
	private Map<String, DictOption> prepLevelOptions;//告警级别
	private Map<String, DictOption> bizSystemOptions;//源系统
	
//	private List<DictOption> deptDict;
	private Map<String, SubmitConf> fileDict;//报送文件字典
	private Map<String, SubmitConf> submitConfs;//报送配置参数
	
	private Map<String, SubmitConf> arapTypeConfs;//A13应收应付类型
	private Map<String, SubmitConf> arapReasonConfs;//A13应收付原因
	private Map<String, SubmitConf> oppParConfs;//A13对方机构
	private Map<String, SubmitConf> mktCodeSipfConfs;//A13市场代码
	private Map<String, SubmitConf> arapBusiTypeConfs;//A13业务类型
	private Map<String, SubmitConf> curTypeSipfConfs;//A13币种
	
	private Map<String, Map<String, SubmitConf>> businessConfs;//A13全业务配置的类型
	
	public static SysCodeDictLoader getInstance() {
		if (singleton == null) {
			singleton = new SysCodeDictLoader();
		}
		return singleton;
	}

	private SysCodeDictLoader() {
		
	}

	/**
	 * 进行初始化数字字典操作
	 * 
	 * @return
	 */
	public boolean initAllCodeDict() {
		try {
			initSysConfig();//加载系统配置
			return true;
		} catch (Exception e) {
			log.error("启动初始化出错。", e);
		}
		return false;
	}
	
	/**
	 * 加载系统配置
	 */
	public void initSysConfig() {
		IETLLogService elSer = (IETLLogService)SpringContextLoader.getBean("etlLogService");
		List<DictOption> doList = elSer.getEtlProcOptions();
		etlProcOptions = new LinkedHashMap<String, DictOption>(doList.size());
		for (DictOption d : doList) {
			etlProcOptions.put(d.getKey(), d);
		}
		
		doList = elSer.getPrepLevelOptions();
		prepLevelOptions = new LinkedHashMap<String, DictOption>(doList.size());
		for (DictOption d : doList) {
			prepLevelOptions.put(d.getKey(), d);
		}
		
		doList = elSer.getBizSystemOptions();
		bizSystemOptions = new LinkedHashMap<String, DictOption>(doList.size());
		for (DictOption d : doList) {
			bizSystemOptions.put(d.getKey(), d);
		}
		
		ISubmitConfService scSer = (ISubmitConfService)SpringContextLoader.getBean("submitConfService");
		List<SubmitConf> scList = scSer.getSubmitConfByType(SubmitConf.SUBMIT_FILE, null);
		fileDict = new LinkedHashMap<String, SubmitConf>(scList.size());
		for (SubmitConf c : scList) {
			fileDict.put(c.getCode(), c);
		}
		
		scList = scSer.getSubmitConfByType(SubmitConf.DATA_SUBMIT, null);
		submitConfs = new LinkedHashMap<String, SubmitConf>(scList.size());
		for (SubmitConf c : scList) {
			submitConfs.put(c.getCode(), c);
		}
		
		scList = scSer.getSubmitConfByType(SubmitConf.A13_YSYFLX, null);
		arapTypeConfs = new LinkedHashMap<String, SubmitConf>(scList.size());
		for (SubmitConf c : scList) {
			arapTypeConfs.put(c.getCode(), c);
		}
		
		scList = scSer.getSubmitConfByType(SubmitConf.A13_YSFYY, null);
		arapReasonConfs = new LinkedHashMap<String, SubmitConf>(scList.size());
		for (SubmitConf c : scList) {
			arapReasonConfs.put(c.getCode(), c);
		}
		
		scList = scSer.getSubmitConfByType(SubmitConf.A13_DFJG, null);
		oppParConfs = new LinkedHashMap<String, SubmitConf>(scList.size());
		for (SubmitConf c : scList) {
			oppParConfs.put(c.getCode(), c);
		}
		
		scList = scSer.getSubmitConfByType(SubmitConf.A13_SCDM, null);
		mktCodeSipfConfs = new LinkedHashMap<String, SubmitConf>(scList.size());
		for (SubmitConf c : scList) {
			mktCodeSipfConfs.put(c.getCode(), c);
		}
		
		scList = scSer.getSubmitConfByType(SubmitConf.A13_YWLX, null);
		arapBusiTypeConfs = new LinkedHashMap<String, SubmitConf>(scList.size());
		for (SubmitConf c : scList) {
			arapBusiTypeConfs.put(c.getCode(), c);
		}
		
		scList = scSer.getSubmitConfByType(SubmitConf.A13_BZ, null);
		curTypeSipfConfs = new LinkedHashMap<String, SubmitConf>(scList.size());
		for (SubmitConf c : scList) {
			curTypeSipfConfs.put(c.getCode(), c);
		}
		
		scList = scSer.getSubmitConfByType(SubmitConf.A13_BUSI_CONF, null);
		businessConfs = new LinkedHashMap<String, Map<String, SubmitConf>>();
		for (SubmitConf c : scList) {
			Map<String, SubmitConf> _map = businessConfs.get(c.getValue());
			if (_map == null) {
				_map = new LinkedHashMap<String, SubmitConf>();
				businessConfs.put(c.getValue(), _map);
			}
			_map.put(c.getCode(), c);
		}
	}

	/**
	 * 取得报送文件字典对照值
	 * @return
	 */
	public String getFileDictText(String key) {
		SubmitConf dict = singleton.fileDict.get(key);
		if (dict == null) {
			return null;
		}
		return dict.getName();
	}
	
	public Collection<SubmitConf> getFileDicts() {
		return singleton.fileDict.values();
	}
	
	/**
	 * 取得报送配置对照值
	 * @param key
	 * @return
	 */
	public String getSubmitConfValue(String key) {
		SubmitConf conf = singleton.submitConfs.get(key);
		if (conf == null) {
			return null;
		}
		return conf.getValue();
	}
	
	/**
	 * 取得A13应收应付类型对照值
	 * @param key
	 * @return
	 */
	public String getArapTypeConfText(String key) {
		SubmitConf conf = singleton.arapTypeConfs.get(key);
		if (conf == null) {
			return null;
		}
		return conf.getName();
	}
	
	public Collection<SubmitConf> getArapTypeConfs() {
		Collection<SubmitConf> temCol = singleton.arapTypeConfs.values();
		if (temCol == null || temCol.isEmpty()){
			return temCol;
		}
		Collection<SubmitConf> col = new ArrayList<SubmitConf>();
		for(SubmitConf sc : temCol) {
			if (SubmitConf.STATUS_EDITABLE.equals(sc.getStatus())) {
				col.add(sc);
			}
		}
		return col;
	}
	
	public Collection<SubmitConf> getAllArapTypeConfs() {
		return singleton.arapTypeConfs.values();
	}
	
	/**
	 * 取得A13应收付原因对照值
	 * @param key
	 * @return
	 */
	public String getArapReasonConfText(String key) {
		SubmitConf conf = singleton.arapReasonConfs.get(key);
		if (conf == null) {
			return null;
		}
		return conf.getName();
	}
	
	public Collection<SubmitConf> getArapReasonConfs() {
		return singleton.arapReasonConfs.values();
	}
	
	/**
	 * 取得A13对方机构对照值
	 * @param key
	 * @return
	 */
	public String getOppParConfText(String key) {
		SubmitConf conf = singleton.oppParConfs.get(key);
		if (conf == null) {
			return null;
		}
		return conf.getName();
	}
	
	public Collection<SubmitConf> getOppParConfs() {
		return singleton.oppParConfs.values();
	}
	
	/**
	 * 取得A13市场代码对照值
	 * @param key
	 * @return
	 */
	public String getMktCodeSipfConfText(String key) {
		SubmitConf conf = singleton.mktCodeSipfConfs.get(key);
		if (conf == null) {
			return null;
		}
		return conf.getName();
	}
	
	public Collection<SubmitConf> getMktCodeSipfConfs() {
		return singleton.mktCodeSipfConfs.values();
	}
	
	/**
	 * 取得A13业务类型对照值
	 * @param key
	 * @return
	 */
	public String getArapBusiTypeConfText(String key) {
		SubmitConf conf = singleton.arapBusiTypeConfs.get(key);
		if (conf == null) {
			return null;
		}
		return conf.getName();
	}
	
	public Collection<SubmitConf> getArapBusiTypeConfs() {
		return singleton.arapBusiTypeConfs.values();
	}
	
	/**
	 * 取得A13币种对照值
	 * @param key
	 * @return
	 */
	public String getCurTypeSipfConfText(String key) {
		SubmitConf conf = singleton.curTypeSipfConfs.get(key);
		if (conf == null) {
			return null;
		}
		return conf.getName();
	}
	
	public Collection<SubmitConf> getCurTypeSipfConfs() {
		return singleton.curTypeSipfConfs.values();
	}
	
	/**
	 * 取得A13全业务配置值的对照值
	 * @param typeKey
	 * @param valueKey
	 * @return
	 */
	public String getBusinessConfText(String typeKey, String valueKey) {
		Map<String, SubmitConf> _map = singleton.businessConfs.get(typeKey);
		if (_map == null || _map.isEmpty()) {
			return null;
		}
		SubmitConf conf = _map.get(valueKey);
		if (conf == null) {
			return null;
		}
		return conf.getName();
	}
	
	public Collection<SubmitConf> getBusinessConfs(String typeKey) {
		Map<String, SubmitConf> _map = singleton.businessConfs.get(typeKey);
		if (_map == null || _map.isEmpty()) {
			return null;
		}
		return _map.values();
	}
	
	/**
	 * ETL过程
	 * @param key
	 * @return
	 */
	public String getEtlProcOptionText(String key) {
		DictOption opt = singleton.etlProcOptions.get(key);
		if (opt == null) {
			return null;
		}
		return opt.getText();
	}
	
	public Collection<DictOption> getEtlProcOptions() {
		return singleton.etlProcOptions.values();
	}
	
	/**
	 * 告警级别
	 * @param key
	 * @return
	 */
	public String getPrepLevelOptionText(String key) {
		DictOption opt = singleton.prepLevelOptions.get(key);
		if (opt == null) {
			return null;
		}
		return opt.getText();
	}
	
	public Collection<DictOption> getPrepLevelOptions() {
		return singleton.prepLevelOptions.values();
	}
	
	/**
	 * 源系统
	 * @param key
	 * @return
	 */
	public String getBizSystemOptionText(String key) {
		DictOption opt = singleton.bizSystemOptions.get(key);
		if (opt == null) {
			return null;
		}
		return opt.getText();
	}
	
	public Collection<DictOption> getBizSystemOptions() {
		return singleton.bizSystemOptions.values();
	}
}
